// pages/apps/poi/point/point-serve/point-serve.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 基础
    windowWidth: getApp().globalData.winWidth,
    servepath: '',
    tabScroll: 0, //使得导航位置居中
    currentTab: 0,  //当前的分类tab
    // currentTypeid: 0, //当前的分类的id

    // 以下业务数据
    baseImgUrl: getApp().globalData.IMG_URL,
    pointInfo: { name: '', logo: '', type: 1, pointStoreClsList: [] },
    pointId: '',
    servePath: '',
    serveTypes: [
      { name: '', id: '1', hasSelected: true, path: '' }
    ],
    maxPageSize: 50,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('pageData', function (param) {
      console.error("param参数：" + param.path)
      // console.error("param参数2：" + JSON.stringify(param.point))
      that.setData({ servePath: param.path })
      that.setData({ pointInfo: param.point })
      that.setData({ pointId: param.point.id })
      console.error("param参数data：" + JSON.stringify(that.data))
      // 获取兴趣点信息
      that.getTradeAreaByID(that.data.pointId);
    })
  },

  //--------------------------------- 导航栏 - 单击
  clickMenu: function (e: any) {
    // tab处理
    var current = e.currentTarget.dataset.current //获取当前tab的index
    var typeid = e.currentTarget.dataset.typeid; //获取当前的类型id
    var typepath = e.currentTarget.dataset.typepath; //获取当前的类型id
    this.setData({ servePath: typepath })
    // var tabWidth = this.data.windowWidth / 5 // 导航tab共5个，获取一个的宽度
    // var typeid = e.currentTarget.dataset.typeid; //获取当前的类型id
    // this.setData({
    //   tabScroll: (current - 2) * tabWidth, //使点击的tab始终在居中位置
    //   currentTypeid: typeid,
    //   currentTab: current
    // })

    // // 子页面处理
    // const item = e.currentTarget.dataset.item
    // this.setData({ servePath: item.path })
    // this.setData({ serveName: item.name })

    // // 加载分页
    // this.getPointPageList(this.data.pointId, typeid, this.data.servePath)
    this.switchTab(current, typeid, this.data.pointId, this.data.servePath);
  },
  /**
   * 切换tabs
   */
  switchTab(index: number, storeClsId: string, pointId: string, servePath: string) {
    // tab处理
    var current = index //获取当前tab的index
    var tabWidth = this.data.windowWidth / 5 // 导航tab共5个，获取一个的宽度
    // var typeid = storeCls; //获取当前的类型id

    this.setData({
      tabScroll: (current - 2) * tabWidth, //使点击的tab始终在居中位置
      // currentTypeid: typeid,
      currentTab: current
    })

    // 子页面处理
    this.setData({ servePath: servePath })
    // this.setData({ serveName: item.name })

    // 加载分页
    this.getPointPageList(pointId, storeClsId, servePath)
  },

  //导航栏 - 单击

  /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
  onPullDownRefresh() {
    console.error("life - onPullDownRefresh")

  },
  /**
     * 页面上拉触底事件的处理函数
     */
  onReachBottom() {
    console.error("life - onReachBottom")
  },

  /**
   * 加载兴趣点及服务
   * @param pointId 
   */
  getTradeAreaByID(pointId: string) {
    var that = this
    const preUrl = getApp().globalData.SERVER_URL
    const url = preUrl + "fc/tradeArea/open/queryOpenInfoById"
    // console.error("url: " + url)
    wx.request({
      url: url,
      method: 'GET',
      data: {
        id: pointId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
      },
      success(res: any) {
        // console.error("结果: " + JSON.stringify(res))
        wx.stopPullDownRefresh()
        if (res.data.code == 200) {
          that.setData({ pointInfo: res.data.result })
          // 显示轮播图部分
          var logoArr = that.data.pointInfo.logo.split(',') as string[]
          that.setData({ scenicImgs: logoArr })

          if (that.data.pointInfo.pointStoreClsList != null && that.data.pointInfo.pointStoreClsList.length > 0) {
            that.setData({ serveTypes: that.data.pointInfo.pointStoreClsList })
          }
          // console.error("结果解析: " + JSON.stringify(that.data.pointInfo))

          // 设置标题
          wx.setNavigationBarTitle({ title: that.data.pointInfo.name })

          // 处理tab切换功能
          var selectedStroeCls = null;
          var currIndex = 0;
          for (let index = 0; index < that.data.serveTypes.length; index++) {
            const tempCls = that.data.serveTypes[index];
            if (that.data.servePath == tempCls.path) {
              currIndex = index;
              selectedStroeCls = tempCls
            }
          }
          // 根据传值，选中某个tab及页面
          if (selectedStroeCls == null) {
            selectedStroeCls = that.data.serveTypes[0];
          }
          // 设置当前的路径
          that.setData({ servePath: selectedStroeCls.path })
          // 切换tab
          that.switchTab(currIndex, selectedStroeCls.id, that.data.pointId, that.data.servePath);
        } else {
          console.error(JSON.stringify(res))
        }
      },
    })
  },
  // -------------------------------------------------------------------------
  /**
    * Tabs数据列表
    * @param pointId 
    */
  getPointPageList(pointId: string, storeClsId: string, path: string) {
    // console.error(pointId + "," + storeClsId + "," + path)
    if ('getParkingPageByTradeAreaId' == this.data.servePath) {
      // 停车场
      this.getParkingPageByTradeAreaId(pointId)
    } else {
      // 默认加载门店数据
      this.getPointHomeStorePageData(pointId, storeClsId, path);
    }
  },

  /**
   * 获取店铺数据
   * @param pointId 
   * @param storeClsId 
   */
  getPointHomeStorePageData(pointId: string, storeClsId: string, path: string) {
    // 选中的tabs
    this.setData({ servePath: path })

    // 加载数据
    var that = this
    const preUrl = getApp().globalData.SERVER_URL
    const url = preUrl + "sys/open/store/getPointHomeStorePageData"
    wx.request({
      url: url,
      method: 'GET',
      data: {
        pageNo: 1,
        pageSize: that.data.maxPageSize,
        pointId: pointId,
        storeClsId: storeClsId,
        lat: getApp().globalData.latitude,
        lng: getApp().globalData.longitude
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
      },
      success(res: any) {
        // console.error("getFoodStorePageByPointId: " + JSON.stringify(res))
        wx.stopPullDownRefresh()
        if (res.data.code == 200) {
          const list = res.data.result.records;
          if (list != null && list.length > 0) {
            for (let index = 0; index < list.length; index++) {
              const item = list[index];
              if (item.storeLogo != null) {
                var logoArr = item.storeLogo.split(',') as string[]
                item.logo = logoArr[0]
              }
            }
          }

          if ('getVipNearbyHotGoodsPageByPointId' == path) {// 热门
            that.setData({ vipHotGoodsList: list })

          } else if ('getFoodStorePageByPointId' == path) {// 美食
            that.setData({ foodStoreList: list })

          } else if ('getHappyStorePageByPointId' == path) { // 休闲娱乐
            that.setData({ happyStoreList: list })

          } else if ('getShoppingStorePageByPointId' == path) { // 购物
            that.setData({ shoppingStoreList: list })

          } else if ('getBeautyworldStorePageByPointId' == path) { // 美容美发
            that.setData({ beautyworldStoreList: list })

          } else {
          }
        } else {
          console.error(JSON.stringify(res))
        }
      },
    })
  },

  /**
  * 获取停车场
  * @param tradeAreaId
  */
  getParkingPageByTradeAreaId(tradeAreaId: string) {
    this.setData({ servePath: 'getParkingPageByTradeAreaId' })
    var that = this
    const preUrl = getApp().globalData.SERVER_URL
    const url = preUrl + "fc/parking/open/pageByTradeAreaId"
    // console.error("@url: " + url)
    wx.request({
      url: url,
      method: 'GET',
      data: {
        pageNo: 1,
        pageSize: that.data.maxPageSize,
        tradeArea: tradeAreaId,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        // 'X-Access-Token': token
      },
      success(res: any) {
        // console.error("@结果: " + JSON.stringify(res))
        wx.stopPullDownRefresh()
        if (res.data.code == 200) {
          const list = res.data.result.records;
          if (list != null && list.length > 0) {
            for (let index = 0; index < list.length; index++) {
              const item = list[index];
              if (item.logo != null) {
                var logoArr = item.logo.split(',') as string[]
                item.logo = logoArr[0]
              }
            }
          }
          that.setData({ parkingList: list })
          // console.error("@结果2: " + JSON.stringify(list))
        } else {
          console.error(JSON.stringify(res))
        }
      },
    })
  },

  // -------------------------------------------------------------------------

  /**
   * 打开导航-停车场
   */
  openMapNavi(param: any) {
    console.error("toParkingNavi:" + JSON.stringify(param))
    const title = param.currentTarget.dataset.title
    const lat = param.currentTarget.dataset.lat
    const lng = param.currentTarget.dataset.lng
    const address = param.currentTarget.dataset.address

    console.error("toParkingNavi: title=" + title + ", lat=" + lat + ",lng=" + lng + ", address=" + address)

    wx.openLocation({
      latitude: lat, // 纬度，范围为-90~90，负数表示南纬
      longitude: lng, // 经度，范围为-180~180，负数表示西经
      scale: 13, // 缩放比例
      name: title,
      address: address,
      success: function (r) {
        console.log(r)
      }
    })
  },

  /**
   * 打开详情-商家
   */
  toStoreDetail(param: any) {
    // console.error("toStoreDetail")
    // wx.navigateTo({
    //   url: '/pages/apps/store/storeDetail/storeDetail',
    //   events: {
    //   },
    //   success: function (res) {
    //     // 通过eventChannel向被打开页面传送数据
    //     res.eventChannel.emit('beanData', { bean: param.currentTarget.dataset.bean })
    //   }
    // })

    const bean = param.currentTarget.dataset.bean
    let storeId = bean.id
    console.error("storeId: " + storeId)
    wx.navigateTo({
      url: '/pages/apps/store/storeHome/storeHome',
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据  
        res.eventChannel.emit('acceptDataFromOpenerPage', { storeId: storeId })
      }
    })
  },

  /**
   * 打开详情-停车场
   */
  toParkingDetail(param: any) {
    console.error("toParkingDetail")
    wx.navigateTo({
      url: '/pages/apps/parking/parking-detail/parking-detail',
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('beanData', { bean: param.currentTarget.dataset.bean })
      }
    })

    // wx.chooseLocation({
    //   latitude: 26.558947702735,
    //   longitude: 106.67310027432,
    //   success: function (res) {
    //     console.error("chooseLocation:success: " + JSON.stringify(res))
    //   },
    //   fail: function (res) {
    //     console.error("chooseLocation:fail: " + JSON.stringify(res))
    //   },
    //   complete: function (res) {
    //   },
    // })
  },
})